import log from 'fancy-log'
import minimist from 'minimist'

const argv = minimist(process.argv.slice(2))

const path = {
  dataPath: 'data',
  src: 'src',
  dest: 'dist'
}


function errorHandler(err) {
  log.error(`${err.name} in ${err.plugin}`, err.message)
  this.emit('end')
}

const config = {
  isProd: argv.prod,
  root: process.cwd(),
  src: path.src,
  dest: path.dest,
  index: 'index.html', // 服务启动访问的
  plumber: {
    errorHandler
  },
  html: {
    dataPath: `${path.dataPath}`,
    dataExt: '.json',
    path: `${path.src}/views`,
    src: `${path.src}/views/*.ejs`,
    dest: path.dest,
    watch: [`${path.src}/views/**/*.ejs`, `${path.dataPath}/**/*.json`]
  },
  style: {
    src: `${path.src}/styles/**/*.less`,
    dest: `${path.dest}/styles/`,
    watch: [`${path.src}/styles/**/*.less`],
    postcss: {
      assets: {
        loadPaths: [
          `${path.dest}/image`,
        ],
        relative: true,
        cache: true,
        cachebuster: true,
      },
      nodeCssMqPacker: {
        sort: (a, b) => a.localeCompare(b), // 按照定义的顺序合并相同的样式规则
      }
    }
  },
  script: {
    src: `${path.src}/script/**/*.js`,
    dest: `${path.dest}/script/`,
    watch: [`${path.src}/script/**/*.js`]
  },
  image: {
    src: `${path.src}/image/**/*`,
    dest: `${path.dest}/image`,
    watch: `${path.src}/image/**/*`,
    imageMin: {
      plugins: [
        // imagemin.gifsicle({interlaced: true}), // interlaced-交错gif渐进呈现
        // imagemin.mozjpeg({quality: 75, progressive: true}),  // quality-压缩质量，在范围0(最差)到100(完美)
        // imagemin.optipng({optimizationLevel: 5}),
        // imagemin.svgo({
        //     plugins: [
        //         {removeViewBox: false},  // 是否尽可能删除viewBox属性
        //         {cleanupIDs: false}  // 删除未使用的id并缩小使用的id
        //     ]
        // })
      ]
    }
  },
  static: {
    src: `${path.src}/static/**/*`,
    dest: `${path.dest}/static`,
    watch: [`${path.src}/static/**/*`]
  }
}

export default config